<?php

namespace app\common\model;

use app\common\model\user\Business;
use think\Model;


class UserCards extends Model
{

    

    

    // 表名
    protected $name = 'user_cards';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'rights_endtime_text',
        'status_text'
    ];



    public function getStatusList()
    {
        return ['1' => __('使用中'), '2' => __('使用完成'),'3'=>'已退款','4'=>'已过期'];
    }

    public function getIsShowList()
    {
        return ['1' => __('否'), '2' => __('是')];
    }
    public function getIsAllList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRightsEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rights_endtime']) ? $data['rights_endtime'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setRightsEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function business()
    {
        return $this->belongsTo(Business::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function services()
    {
        return $this->hasMany(Cardxamines::class,'card_id','id')->field('id,card_id,tag_name');
    }
}
